<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>react原理 | 七里香</title><meta name="keywords" content="react"><meta name="author" content="william"><meta name="copyright" content="william"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="react原理 深入掌握虚拟dom 掌握render、Component两个基础核心api 掌握常见组件的渲染 掌握虚拟dom、diff策略 掌握fiber原理及实现  知识点React 本身只是一个 DOM 的抽象层，使用组件构建虚拟 DOM。 虚拟dom常见问题：react virtual dom是什么？说一下diff算法？what？用 JavaScript 对象表示 DOM 信息和结构，当状">
<meta property="og:type" content="article">
<meta property="og:title" content="react原理">
<meta property="og:url" content="http://yokeney.gitee.io/my-hexo-blog/2022/10/15/react%E5%8E%9F%E7%90%86/index.html">
<meta property="og:site_name" content="七里香">
<meta property="og:description" content="react原理 深入掌握虚拟dom 掌握render、Component两个基础核心api 掌握常见组件的渲染 掌握虚拟dom、diff策略 掌握fiber原理及实现  知识点React 本身只是一个 DOM 的抽象层，使用组件构建虚拟 DOM。 虚拟dom常见问题：react virtual dom是什么？说一下diff算法？what？用 JavaScript 对象表示 DOM 信息和结构，当状">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://yokeney.gitee.io/my-hexo-blog/img/reactfiber.png">
<meta property="article:published_time" content="2022-10-15T13:37:37.000Z">
<meta property="article:modified_time" content="2022-10-15T13:40:06.820Z">
<meta property="article:author" content="william">
<meta property="article:tag" content="react">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://yokeney.gitee.io/my-hexo-blog/img/reactfiber.png"><link rel="shortcut icon" href="/my-hexo-blog/img/index.ico"><link rel="canonical" href="http://yokeney.gitee.io/my-hexo-blog/2022/10/15/react%E5%8E%9F%E7%90%86/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/my-hexo-blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/my-hexo-blog/',
  algolia: undefined,
  localSearch: {"path":"/my-hexo-blog/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'react原理',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-10-15 21:40:06'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/my-hexo-blog/img/2.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/my-hexo-blog/archives/"><div class="headline">文章</div><div class="length-num">17</div></a><a href="/my-hexo-blog/tags/"><div class="headline">标签</div><div class="length-num">13</div></a><a href="/my-hexo-blog/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my-hexo-blog/"><i class="fa-fw iconfont icon-shouyex"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw iconfont icon-zuixinwenzhang_huaban"></i><span> 分类</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/my-hexo-blog/vueResources/"><i class="fa-fw iconfont icon-fenlei1"></i><span> vue</span></a></li><li><a class="site-page child" href="/my-hexo-blog/reactResources/"><i class="fa-fw iconfont icon-fenlei1"></i><span> react</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw iconfont icon-shenghuo"></i><span> 后台</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/my-hexo-blog/nodeResources/"><i class="fa-fw iconfont icon-fenlei1"></i><span> node</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/my-hexo-blog/img/reactfiber.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/my-hexo-blog/">七里香</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my-hexo-blog/"><i class="fa-fw iconfont icon-shouyex"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw iconfont icon-zuixinwenzhang_huaban"></i><span> 分类</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/my-hexo-blog/vueResources/"><i class="fa-fw iconfont icon-fenlei1"></i><span> vue</span></a></li><li><a class="site-page child" href="/my-hexo-blog/reactResources/"><i class="fa-fw iconfont icon-fenlei1"></i><span> react</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw iconfont icon-shenghuo"></i><span> 后台</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/my-hexo-blog/nodeResources/"><i class="fa-fw iconfont icon-fenlei1"></i><span> node</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">react原理</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-10-15T13:37:37.000Z" title="发表于 2022-10-15 21:37:37">2022-10-15</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-10-15T13:40:06.820Z" title="更新于 2022-10-15 21:40:06">2022-10-15</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="react原理"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="react原理"><a href="#react原理" class="headerlink" title="react原理"></a>react原理</h1><ol>
<li>深入掌握虚拟dom</li>
<li>掌握render、Component两个基础核心api</li>
<li>掌握常见组件的渲染</li>
<li>掌握虚拟dom、diff策略</li>
<li>掌握fiber原理及实现</li>
</ol>
<h1 id="知识点"><a href="#知识点" class="headerlink" title="知识点"></a>知识点</h1><p>React 本身只是一个 DOM 的抽象层，使用组件构建虚拟 DOM。</p>
<h2 id="虚拟dom"><a href="#虚拟dom" class="headerlink" title="虚拟dom"></a>虚拟dom</h2><h2 id="常见问题：react-virtual-dom是什么？说一下diff算法？"><a href="#常见问题：react-virtual-dom是什么？说一下diff算法？" class="headerlink" title="常见问题：react virtual dom是什么？说一下diff算法？"></a>常见问题：react virtual dom是什么？说一下diff算法？</h2><p><code>what？</code>用 JavaScript 对象表示 DOM 信息和结构，当状态变更的时候，重新渲染这个 JavaScript 的对象结构。这个 JavaScript 对象称为virtual dom；</p>
<h2 id="传统dom渲染流程"><a href="#传统dom渲染流程" class="headerlink" title="传统dom渲染流程"></a>传统dom渲染流程</h2><p><code>why？</code>DOM操作很慢，轻微的操作都可能导致页面重新排版，非常耗性能。相对于DOM对象，js对象处理起来更快，而且更简单。通过diff算法对比新旧vdom之间的差异，可以批量的、最小化的执行dom操作，从而提高性能。</p>
<p><code>where？</code>React中用JSX语法描述视图(View)，通过babel-loader转译后它们变为React.createElement(…)形式，该函数将生成vdom来描述真实dom。将来如果状态变化，vdom将作出相应变化，再通过diff算法对比新老vdom区别从而做出最终dom操作。</p>
<h2 id="JSX"><a href="#JSX" class="headerlink" title="JSX"></a>JSX</h2><h3 id="1-什么是JSX"><a href="#1-什么是JSX" class="headerlink" title="1. 什么是JSX"></a>1. 什么是JSX</h3><p>语法糖</p>
<p>React 使用 JSX 来替代常规的 JavaScript。</p>
<p>JSX 是一个看起来很像 XML 的 JavaScript 语法扩展。</p>
<h3 id="2-为什么需要JSX"><a href="#2-为什么需要JSX" class="headerlink" title="2. 为什么需要JSX"></a>2. 为什么需要JSX</h3><p>开发效率：使用 JSX 编写模板简单快速。</p>
<p>执行效率：JSX编译为 JavaScript 代码后进行了优化，执行更快。</p>
<p>类型安全：在编译过程中就能发现错误。</p>
<h3 id="3-React-16原理"><a href="#3-React-16原理" class="headerlink" title="3. React 16原理"></a>3. React 16原理</h3><p><code>babel-loader</code>会预编译JSX为<code>React.createElement(...)</code></p>
<h3 id="4-React-17原理"><a href="#4-React-17原理" class="headerlink" title="4. React 17原理"></a>4. React 17原理</h3><p>React 17中的 JSX 转换不会将 JSX 转换为 React.createElement，而是自动从React 的 package 中引入新的入口函数并调用。另外此次升级不会改变 JSX 语法，旧的 JSX 转换也将继续工作。</p>
<h3 id="5-与vue的异同："><a href="#5-与vue的异同：" class="headerlink" title="5. 与vue的异同："></a>5. 与vue的异同：</h3><p>react中虚拟dom+jsx的设计一开始就有，vue则是演进过程中才出现的</p>
<p>jsx本来就是js扩展，转义过程简单直接的多；vue把template编译为render函数的过程需要<code>复杂的编译器转换字符串-&gt;ast-&gt;js函数字符串</code></p>
<p>核心精简后：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="title class_">React</span> = &#123;</span><br><span class="line">createElement,</span><br><span class="line"><span class="title class_">Component</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="核心的api"><a href="#核心的api" class="headerlink" title="核心的api"></a>核心的api</h3><ul>
<li>React.Component：实现自定义组件</li>
<li>ReactDOM.render：渲染真实DOM</li>
</ul>
<h3 id="ReactDOM"><a href="#ReactDOM" class="headerlink" title="ReactDOM"></a>ReactDOM</h3><ul>
<li>render()</li>
<li>ReactDOM.render(element, container[, callback])</li>
</ul>
<p>当首次调用时，容器节点里的所有 DOM 元素都会被替换，后续的调用则会使用 React 的 DOM 差分算法（DOM diffing algorithm）进行高效的更新。如果提供了可选的回调函数，该回调将在组件被渲染或更新之后被执行。</p>
<h3 id="节点类型"><a href="#节点类型" class="headerlink" title="节点类型"></a>节点类型</h3><h4 id="注意节点类型："><a href="#注意节点类型：" class="headerlink" title="注意节点类型："></a>注意节点类型：</h4><ul>
<li>文本节点</li>
<li>HTML标签节点</li>
<li>函数组件</li>
<li>类组件</li>
<li>函数组件</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">Welcome</span>(<span class="params">props</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="language-xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>Hello, &#123;props.name&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="类组件"><a href="#类组件" class="headerlink" title="类组件"></a>类组件</h2><p>React 的组件可以定义为 class 或函数的形式。如需定义 class 组件，需要继承 <code>React.Component</code> 或者<code> React.PureComponent</code> ：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Welcome</span> <span class="keyword">extends</span> <span class="title class_ inherited__">React.Component</span> &#123;</span><br><span class="line"></span><br><span class="line"><span class="title function_">render</span>(<span class="params"></span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="language-xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>Hello, &#123;this.props.name&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="类组件源码"><a href="#类组件源码" class="headerlink" title="类组件源码"></a>类组件源码</h3><p><strong>实现ReactDom.render, Component</strong></p>
<p>src&#x2F;index.js</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// import ReactDOM from &quot;react-dom&quot;;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">ReactDOM</span> <span class="keyword">from</span> <span class="string">&quot;react-dom&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Component</span> <span class="keyword">from</span> <span class="string">&quot;Component&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;./index.css&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">FunctionComponent</span>(<span class="params">props</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> (</span><br><span class="line"></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&quot;border&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">p</span>&gt;</span>&#123;props.name&#125;<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"></span><br><span class="line">);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">ClassComponent</span> <span class="keyword">extends</span> <span class="title class_ inherited__">Component</span> &#123;</span><br><span class="line"></span><br><span class="line"><span class="title function_">render</span>(<span class="params"></span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> (</span><br><span class="line"></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&quot;border&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">p</span>&gt;</span>&#123;this.props.name&#125;<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span>全栈工程师</span><br><span class="line"></span><br><span class="line">修改index.<span class="property">js</span>实际引入react，测试</span><br><span class="line"></span><br><span class="line"><span class="title class_">ReactDOM</span>.<span class="property">render</span></span><br><span class="line"></span><br><span class="line">);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">FragmentComponent</span>(<span class="params">props</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> (</span><br><span class="line"></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>111<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>222<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;/&gt;</span></span></span><br><span class="line"></span><br><span class="line">);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> jsx = (</span><br><span class="line"></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&quot;border&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>慢慢慢<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>全栈<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">FunctionComponent</span> <span class="attr">name</span>=<span class="string">&quot;函数组件&quot;</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">ClassComponent</span> <span class="attr">name</span>=<span class="string">&quot;类组件&quot;</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">FragmentComponent</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"></span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="title class_">ReactDOM</span>.<span class="title function_">render</span>(jsx, <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;root&quot;</span>));</span><br></pre></td></tr></table></figure>


<h3 id="ReactDOM-render"><a href="#ReactDOM-render" class="headerlink" title="ReactDOM.render"></a>ReactDOM.render</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">ReactDOM</span> <span class="keyword">from</span> <span class="string">&quot;react-dom&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Component</span> <span class="keyword">from</span> <span class="string">&quot;Component&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// vnode 虚拟dom节点</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// node 真实dom节点</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">render</span>(<span class="params">vnode, container</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;vnode&quot;</span>, vnode); <span class="comment">//sy-log</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// step1 : vnode-&gt;node</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> node = <span class="title function_">createNode</span>(vnode);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 把node更新到container中</span></span><br><span class="line"></span><br><span class="line">container.<span class="title function_">appendChild</span>(node);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">isStringOrNumber</span>(<span class="params">sth</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="keyword">typeof</span> sth === <span class="string">&quot;string&quot;</span> || <span class="keyword">typeof</span> sth === <span class="string">&quot;number&quot;</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 根据vnode生成node节点</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">createNode</span>(<span class="params">vnode</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> node;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> &#123;type&#125; = vnode;</span><br><span class="line"></span><br><span class="line"><span class="comment">// todo 根据vnode生成node</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (<span class="keyword">typeof</span> type === <span class="string">&quot;string&quot;</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 原生标签节点</span></span><br><span class="line"></span><br><span class="line">node = <span class="title function_">updateHostComponent</span>(vnode);</span><br><span class="line"></span><br><span class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="title function_">isStringOrNumber</span>(vnode)) &#123;</span><br><span class="line"></span><br><span class="line">node = <span class="title function_">updateTextComponent</span>(vnode + <span class="string">&quot;&quot;</span>);</span><br><span class="line"></span><br><span class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="keyword">typeof</span> type === <span class="string">&quot;function&quot;</span>) &#123;</span><br><span class="line"></span><br><span class="line">node = type.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">isReactComponent</span></span><br><span class="line"></span><br><span class="line">? <span class="title function_">updateClassComponent</span>(vnode)</span><br><span class="line"></span><br><span class="line">: <span class="title function_">updateFunctionComponent</span>(vnode);</span><br><span class="line"></span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line"></span><br><span class="line">node = <span class="title function_">updateFragmentComponent</span>(vnode);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> node;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 更新原生标签的，即根据原生标签的vnode生成node</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">updateHostComponent</span>(<span class="params">vnode</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> &#123;type, props&#125; = vnode;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> node = <span class="variable language_">document</span>.<span class="title function_">createElement</span>(type);</span><br><span class="line"></span><br><span class="line"><span class="title function_">updateNode</span>(node, props);</span><br><span class="line"></span><br><span class="line"><span class="title function_">reconcileChildren</span>(node, props.<span class="property">children</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> node;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 更新原生标签的属性，如className、href、id、（style、事件）等</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">updateNode</span>(<span class="params">node, nextVal</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="title class_">Object</span>.<span class="title function_">keys</span>(nextVal)</span><br><span class="line"></span><br><span class="line">.<span class="title function_">filter</span>(<span class="function"><span class="params">k</span> =&gt;</span> k !== <span class="string">&quot;children&quot;</span>)</span><br><span class="line"></span><br><span class="line">.<span class="title function_">forEach</span>(<span class="function"><span class="params">k</span> =&gt;</span> (node[k] = nextVal[k]));</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 函数组件 执行函数</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 返回node</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">updateFunctionComponent</span>(<span class="params">vnode</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> &#123;type, props&#125; = vnode;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> child = <span class="title function_">type</span>(props);</span><br><span class="line"></span><br><span class="line"><span class="comment">// child-&gt;node</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> node = <span class="title function_">createNode</span>(child);</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> node;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 类组件</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 先实例化 再执行render函数</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">updateClassComponent</span>(<span class="params">vnode</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> &#123;type, props&#125; = vnode;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> instance = <span class="keyword">new</span> <span class="title function_">type</span>(props);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> child = instance.<span class="title function_">render</span>();</span><br><span class="line"></span><br><span class="line"><span class="comment">// child-&gt;node</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> node = <span class="title function_">createNode</span>(child);</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> node;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 文本节点</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">updateTextComponent</span>(<span class="params">vnode</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> node = <span class="variable language_">document</span>.<span class="title function_">createTextNode</span>(vnode);</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> node;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 实现Fragment</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">updateFragmentComponent</span>(<span class="params">vnode</span>) &#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 遍历子节点，假的协调</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">reconcileChildren</span>(<span class="params">parentNode, children</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> newChildren = <span class="title class_">Array</span>.<span class="title function_">isArray</span>(children) ? children : [children];</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; newChildren.<span class="property">length</span>; i++) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> child = newChildren[i];</span><br><span class="line"></span><br><span class="line"><span class="comment">// child是vnode, child-&gt;node, 把node更新到parentNode中</span></span><br><span class="line"></span><br><span class="line"><span class="title function_">render</span>(child, parentNode);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;render&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="Component"><a href="#Component" class="headerlink" title="Component"></a>Component</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Component</span> &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">static</span> isReactComponent = &#123;&#125;;</span><br><span class="line"></span><br><span class="line"><span class="title function_">constructor</span>(<span class="params">props</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">this</span>.<span class="property">props</span> = props;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// function Component(props) &#123;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// this.props = props;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// &#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// Component.prototype.isReactComponent = &#123;&#125;;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="title class_">Component</span>;</span><br></pre></td></tr></table></figure>

<p>总结：</p>
<ol>
<li><p>React17中，React会自动替换JSX为js对象</p>
</li>
<li><p>JS对象即vdom，它能够完整描述dom结构</p>
</li>
<li><p>ReactDOM.render(vdom, container)可以将vdom转换为dom并追加到container中</p>
</li>
<li><p>实际上，转换过程需要经过一个diff过程。</p>
</li>
</ol>
<h2 id="reconciliation协调"><a href="#reconciliation协调" class="headerlink" title="reconciliation协调"></a>reconciliation协调</h2><h3 id="设计动力"><a href="#设计动力" class="headerlink" title="设计动力"></a>设计动力</h3><p>在某一时间节点调用 React 的 render() 方法，会创建一棵由 React 元素组成的树。在下一次 state 或props 更新时，相同的 render() 方法会返回一棵不同的树。React 需要基于这两棵树之间的差别来判断如何有效率的更新 UI 以保证当前 UI 与最新的树保持同步。</p>
<p>这个算法问题有一些通用的解决方案，即生成将一棵树转换成另一棵树的最小操作数。 然而，即使在最<br>前沿的算法中，该算法的复杂程度为 O(n 3 )，其中 n 是树中元素的数量。</p>
<p>如果在 React 中使用了该算法，那么展示 1000 个元素所需要执行的计算量将在十亿的量级范围。这个<br>开销实在是太过高昂。于是 React 在以下两个假设的基础之上提出了一套 O(n) 的启发式算法：</p>
<ol>
<li><p>两个不同类型的元素会产生出不同的树；</p>
</li>
<li><p>开发者可以通过 key prop 来暗示哪些子元素在不同的渲染下能保持稳定；</p>
</li>
</ol>
<p>在实践中，我们发现以上假设在几乎所有实用的场景下都成立。</p>
<p>diffing算法</p>
<p>算法复杂度O(n)</p>
<h3 id="diff-策略"><a href="#diff-策略" class="headerlink" title="diff 策略"></a>diff 策略</h3><ol>
<li><p>同级比较，Web UI 中 DOM 节点跨层级的移动操作特别少，可以忽略不计。</p>
</li>
<li><p>拥有不同类型的两个组件将会生成不同的树形结构。例如：div-&gt;p, CompA-&gt;CompB</p>
</li>
<li><p>开发者可以通过 key prop 来暗示哪些子元素在不同的渲染下能保持稳定；</p>
</li>
</ol>
<h2 id="diff过程"><a href="#diff过程" class="headerlink" title="diff过程"></a>diff过程</h2><p>比对两个虚拟dom时会有三种操作：删除、替换和更新</p>
<p>vnode是现在的虚拟dom，newVnode是新虚拟dom。</p>
<ol>
<li>删除：newVnode不存在时</li>
<li>替换：vnode和newVnode类型不同或key不同时</li>
<li>更新：有相同类型和key但vnode和newVnode不同时</li>
</ol>
<p><code>在实践中也证明这三个前提策略是合理且准确的，它保证了整体界面构建的性能。</code></p>
<h2 id="fiber"><a href="#fiber" class="headerlink" title="fiber"></a>fiber</h2><h3 id="为什么需要fiber"><a href="#为什么需要fiber" class="headerlink" title="为什么需要fiber"></a>为什么需要fiber</h3><p>对于大型项目，组件树会很大，这个时候递归遍历的成本就会很高，会造成主线程被持续占用，结果就是主线程上的布局、动画等周期性任务就无法立即得到处理，造成视觉上的卡顿，影响用户体验。</p>
<h3 id="2-任务分解的意义"><a href="#2-任务分解的意义" class="headerlink" title="2. 任务分解的意义"></a>2. 任务分解的意义</h3><p>解决上面的问题</p>
<ol>
<li>增量渲染（把渲染任务拆分成块，匀到多帧）</li>
<li>更新时能够暂停，终止，复用渲染任务</li>
<li>给不同类型的更新赋予优先级</li>
<li>并发方面新的基础能力更流畅</li>
</ol>
<h3 id="什么是fiber"><a href="#什么是fiber" class="headerlink" title="什么是fiber"></a>什么是fiber</h3><p>A Fiber is work on a Component that needs to be done or was done. There can be more than one per component.</p>
<p>fiber是指组件上将要完成或者已经完成的任务，每个组件可以一个或者多个。</p>
<p>实现fiber</p>
<p><code>window.requestIdleCallback(callback[, options])</code></p>
<p><code>window.requestIdleCallback()</code>方法将在浏览器的空闲时段内调用的函数排队。这使开发者能够在主事件循环上执行后台和低优先级工作，而不会影响延迟关键事件，如动画和输入响应。函数一般会按先<br>进先调用的顺序执行，然而，如果回调函数指定了执行超时时间 timeout ，则有可能为了在超时前执行<br>函数而打乱执行顺序。</p>
<p>你可以在空闲回调函数中调用 requestIdleCallback() ，以便在下一次通过事件循环之前调度另一个<br>回调。</p>
<h4 id="callback"><a href="#callback" class="headerlink" title="callback"></a>callback</h4><p>一个在事件循环空闲时即将被调用的函数的引用。函数会接收到一个名为 IdleDeadline 的参数，这</p>
<p>个参数可以获取当前空闲时间以及回调是否在超时时间前已经执行的状态。</p>
<h4 id="options-可选"><a href="#options-可选" class="headerlink" title="options 可选"></a>options 可选</h4><p>包括可选的配置参数。具有如下属性：</p>
<p><code>timeout</code> ：如果指定了timeout并具有一个正值，并且尚未通过超时毫秒数调用回调，那么回调会</p>
<p>在下一次空闲时期被强制执行，尽管这样很可能会对性能造成负面影响。</p>
<p><code>Fiber</code> 是 React 16 中新的协调引擎。它的主要目的是使 <code>Virtual DOM</code> 可以进行增量式渲染。</p>
<p>一个更新过程可能被打断，所以React Fiber一个更新过程被分为两个阶段(Phase)：</p>
<blockquote>
<p>第一个阶段:Reconciliation Phase</p>
</blockquote>
<blockquote>
<p> 第二阶段:Commit Phase。</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// import React, &#123;Component&#125; from &quot;react&quot;;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// import ReactDOM from &quot;react-dom&quot;;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">ReactDOM</span> <span class="keyword">from</span> <span class="string">&quot;react-dom&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Component</span> <span class="keyword">from</span> <span class="string">&quot;Component&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;./index.css&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">ClassComponent</span> <span class="keyword">extends</span> <span class="title class_ inherited__">Component</span> &#123;</span><br><span class="line"></span><br><span class="line"><span class="title function_">render</span>(<span class="params"></span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> (</span><br><span class="line"></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&quot;border&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">p</span>&gt;</span>&#123;this.props.name&#125;<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"></span><br><span class="line">);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">FunctionComponent</span>(<span class="params">props</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> (</span><br><span class="line"></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&quot;border&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">p</span>&gt;</span>&#123;props.name&#125;<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">button</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml"></span></span></span><br><span class="line"><span class="tag"><span class="language-xml"><span class="attr">onClick</span>=<span class="string">&#123;()</span> =&gt;</span> &#123;</span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml">console.log(&quot;omg&quot;); //sy-log</span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml">&#125;&#125;&gt;</span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml">click</span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"></span><br><span class="line">);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> jsx = (</span><br><span class="line"></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&quot;border&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>全栈<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">FunctionComponent</span> <span class="attr">name</span>=<span class="string">&quot;function&quot;</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">ClassComponent</span> <span class="attr">name</span>=<span class="string">&quot;class&quot;</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>omg<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>omg<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;/&gt;</span></span></span><br><span class="line"></span><br><span class="line">&lt;/div&gt;</span><br><span class="line">);</span><br><span class="line"><span class="title class_">ReactDOM</span>.<span class="title function_">render</span>(jsx, <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;root&quot;</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">// console.log(&quot;React&quot;, React.version); //sy-log</span></span><br></pre></td></tr></table></figure>

<h2 id="createFiber-js"><a href="#createFiber-js" class="headerlink" title="createFiber.js"></a>createFiber.js</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123;<span class="title class_">Placement</span>&#125; <span class="keyword">from</span> <span class="string">&quot;./utils&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">* fiber:</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">* type 标记节点类型</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">* key 标记节点在当前层级下的唯一性</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">* props 属性</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">* index 标记当前层级下的位置</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">* child 第一个子节点</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">* sibling 下一个兄弟节点</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">* return 父节点</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">* stateNode 如果组件是原生标签则是dom节点，如果是类组件则是类实例</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">createFiber</span>(<span class="params">vnode, returnFiber</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> newFiber = &#123;</span><br><span class="line"></span><br><span class="line"><span class="attr">type</span>: vnode.<span class="property">type</span>,</span><br><span class="line"></span><br><span class="line"><span class="attr">key</span>: vnode.<span class="property">key</span>,</span><br><span class="line"></span><br><span class="line"><span class="attr">props</span>: vnode.<span class="property">props</span>,</span><br><span class="line"></span><br><span class="line"><span class="attr">stateNode</span>: <span class="literal">null</span>,</span><br><span class="line"></span><br><span class="line"><span class="attr">child</span>: <span class="literal">null</span>,</span><br><span class="line"></span><br><span class="line"><span class="attr">return</span>: returnFiber,</span><br><span class="line"></span><br><span class="line"><span class="attr">sibling</span>: <span class="literal">null</span>,</span><br><span class="line"></span><br><span class="line"><span class="attr">alternate</span>: <span class="literal">null</span>,</span><br><span class="line"></span><br><span class="line"><span class="attr">flags</span>: <span class="title class_">Placement</span>,</span><br><span class="line"></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> newFiber;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="react-dom-js"><a href="#react-dom-js" class="headerlink" title="react-dom.js"></a>react-dom.js</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123;scheduleUpdateOnFiber&#125; <span class="keyword">from</span> <span class="string">&quot;./ReactFiberWorkLoop&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> wipRoot = <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">render</span>(<span class="params">vnode, container</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="title class_">FiberRoot</span> = &#123;</span><br><span class="line"></span><br><span class="line"><span class="attr">type</span>: container.<span class="property">nodeName</span>.<span class="title function_">toLocaleLowerCase</span>(),</span><br><span class="line"></span><br><span class="line"><span class="attr">stateNode</span>: container,</span><br><span class="line"></span><br><span class="line"><span class="attr">props</span>: &#123;<span class="attr">children</span>: vnode&#125;,</span><br><span class="line"></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="title function_">scheduleUpdateOnFiber</span>(<span class="title class_">FiberRoot</span>);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;render&#125;;</span><br></pre></td></tr></table></figure>


<h3 id="ReactFiberWorkLoop-js"><a href="#ReactFiberWorkLoop-js" class="headerlink" title="ReactFiberWorkLoop.js"></a>ReactFiberWorkLoop.js</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123;</span><br><span class="line"></span><br><span class="line">updateClassComponent,</span><br><span class="line"></span><br><span class="line">updateFragementComponent,</span><br><span class="line"></span><br><span class="line">updateFunctionComponent,</span><br><span class="line"></span><br><span class="line">updateHostComponent,</span><br><span class="line"></span><br><span class="line">&#125; <span class="keyword">from</span> <span class="string">&quot;./ReactFiberReconciler&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123;isFn, isStr, <span class="title class_">NoFlags</span>, <span class="title class_">Placement</span>&#125; <span class="keyword">from</span> <span class="string">&quot;./utils&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> wipRoot = <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> nextUnitOfWork = <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">scheduleUpdateOnFiber</span>(<span class="params">fiber</span>) &#123;</span><br><span class="line"></span><br><span class="line">wipRoot = fiber;</span><br><span class="line"></span><br><span class="line">wipRoot.<span class="property">sibling</span> = <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line">nextUnitOfWork = wipRoot;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">performUnitOfWork</span>(<span class="params">wip</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> &#123;type&#125; = wip;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (<span class="title function_">isFn</span>(type)) &#123;</span><br><span class="line"></span><br><span class="line">type.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">isReactComponent</span></span><br><span class="line"></span><br><span class="line">? <span class="title function_">updateClassComponent</span>(wip)</span><br><span class="line"></span><br><span class="line">: <span class="title function_">updateFunctionComponent</span>(wip);</span><br><span class="line"></span><br><span class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="title function_">isStr</span>(type)) &#123;</span><br><span class="line"></span><br><span class="line"><span class="title function_">updateHostComponent</span>(wip);</span><br><span class="line"></span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line"></span><br><span class="line"><span class="title function_">updateFragementComponent</span>(wip);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (wip.<span class="property">child</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> wip.<span class="property">child</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> (wip) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (wip.<span class="property">sibling</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> wip.<span class="property">sibling</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">wip = wip.<span class="property">return</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">workLoop</span>(<span class="params">IdleDeadline</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> (nextUnitOfWork &amp;&amp; <span class="title class_">IdleDeadline</span>.<span class="title function_">timeRemaining</span>() &gt; <span class="number">0</span>) &#123;</span><br><span class="line"></span><br><span class="line">nextUnitOfWork = <span class="title function_">performUnitOfWork</span>(nextUnitOfWork);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (!nextUnitOfWork &amp;&amp; wipRoot) &#123;</span><br><span class="line"></span><br><span class="line"><span class="title function_">commitRoot</span>();</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="title class_">ReactFiberReconciler</span>.<span class="property">js</span></span><br><span class="line"></span><br><span class="line"><span class="title function_">requestIdleCallback</span>(workLoop);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 提交</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">commitRoot</span>(<span class="params"></span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="title function_">commitWorker</span>(wipRoot.<span class="property">child</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// wipRoot = null;</span></span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">commitWorker</span>(<span class="params">wip</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (!wip) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> &#123;stateNode, flags&#125; = wip;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> parentNode = <span class="title function_">getParentNode</span>(wip);</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (stateNode &amp;&amp; flags &amp; <span class="title class_">Placement</span>) &#123;</span><br><span class="line"></span><br><span class="line">parentNode.<span class="title function_">appendChild</span>(stateNode);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">wip.<span class="property">flags</span> = <span class="title class_">NoFlags</span>;</span><br><span class="line"></span><br><span class="line"><span class="title function_">commitWorker</span>(wip.<span class="property">child</span>);</span><br><span class="line"></span><br><span class="line"><span class="title function_">commitWorker</span>(wip.<span class="property">sibling</span>);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">getParentNode</span>(<span class="params">fiber</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> (fiber) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (fiber.<span class="property">return</span>.<span class="property">stateNode</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> fiber.<span class="property">return</span>.<span class="property">stateNode</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">fiber = fiber.<span class="property">return</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="ReactFiberReconciler-js"><a href="#ReactFiberReconciler-js" class="headerlink" title="ReactFiberReconciler.js"></a>ReactFiberReconciler.js</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123;createFiber&#125; <span class="keyword">from</span> <span class="string">&quot;./fiber&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123;isArray, isStringOrNumber, updateNode&#125; <span class="keyword">from</span> <span class="string">&quot;./utils&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">updateHostComponent</span>(<span class="params">wip</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (!wip.<span class="property">stateNode</span>) &#123;</span><br><span class="line"></span><br><span class="line">wip.<span class="property">stateNode</span> = <span class="variable language_">document</span>.<span class="title function_">createElement</span>(wip.<span class="property">type</span>);</span><br><span class="line"></span><br><span class="line"><span class="title function_">updateNode</span>(wip.<span class="property">stateNode</span>, &#123;&#125;, wip.<span class="property">props</span>);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="title function_">reconcileChildren</span>(wip, wip.<span class="property">props</span>.<span class="property">children</span>);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">updateFunctionComponent</span>(<span class="params">wip</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> &#123;type, props&#125; = wip;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> child = <span class="title function_">type</span>(props);</span><br><span class="line"></span><br><span class="line"><span class="title function_">reconcileChildren</span>(wip, child);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">utils.<span class="property">js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">updateClassComponent</span>(<span class="params">wip</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> &#123;type, props&#125; = wip;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> instance = <span class="keyword">new</span> <span class="title function_">type</span>(props);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> child = instance.<span class="title function_">render</span>();</span><br><span class="line"></span><br><span class="line"><span class="title function_">reconcileChildren</span>(wip, child);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">updateFragementComponent</span>(<span class="params">wip</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="title function_">reconcileChildren</span>(wip, wip.<span class="property">props</span>.<span class="property">children</span>);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">reconcileChildren</span>(<span class="params">returnFiber, children</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (<span class="title function_">isStringOrNumber</span>(children)) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> newChildren = <span class="title function_">isArray</span>(children) ? children : [children];</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> previousNewFiber = <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; newChildren.<span class="property">length</span>; i++) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> child = newChildren[i];</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> newFiber = <span class="title function_">createFiber</span>(child, returnFiber);</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (previousNewFiber === <span class="literal">null</span>) &#123;</span><br><span class="line"></span><br><span class="line">returnFiber.<span class="property">child</span> = newFiber;</span><br><span class="line"></span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line"></span><br><span class="line">previousNewFiber.<span class="property">sibling</span> = newFiber;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">previousNewFiber = newFiber;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="utils-js"><a href="#utils-js" class="headerlink" title="utils.js"></a>utils.js</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ! flags</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title class_">NoFlags</span> = <span class="comment">/* */</span> <span class="number">0b00000000000000000000</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title class_">Placement</span> = <span class="comment">/* */</span> <span class="number">0b0000000000000000000010</span>; <span class="comment">// 2</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title class_">Update</span> = <span class="comment">/* */</span> <span class="number">0b0000000000000000000100</span>; <span class="comment">// 4</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title class_">Deletion</span> = <span class="comment">/* */</span> <span class="number">0b0000000000000000001000</span>; <span class="comment">// 8</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"></span><br><span class="line">********************************************************************************</span><br><span class="line"></span><br><span class="line">***********</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">isFn</span>(<span class="params">fn</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="keyword">typeof</span> fn === <span class="string">&quot;function&quot;</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">isStr</span>(<span class="params">s</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="keyword">typeof</span> s === <span class="string">&quot;string&quot;</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">isStringOrNumber</span>(<span class="params">s</span>) &#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="keyword">typeof</span> s === <span class="string">&quot;string&quot;</span> || <span class="keyword">typeof</span> s === <span class="string">&quot;number&quot;</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></article><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/my-hexo-blog/tags/react/">react</a></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/my-hexo-blog/2022/10/25/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E5%86%99%E4%B8%80%E4%B8%AAbabel/"><img class="prev-cover" src="/my-hexo-blog/img/babel.webp" onerror="onerror=null;src='/my-hexo-blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">手把手教你写babel插件</div></div></a></div><div class="next-post pull-right"><a href="/my-hexo-blog/2022/10/15/vue3/"><img class="next-cover" src="/my-hexo-blog/img/vue3.png" onerror="onerror=null;src='/my-hexo-blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Vue3 Composition API 快速上手</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/my-hexo-blog/2021/01/15/react/" title="react入门基础篇"><img class="cover" src="/my-hexo-blog/img/react.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-15</div><div class="title">react入门基础篇</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/my-hexo-blog/img/2.png" onerror="this.onerror=null;this.src='/my-hexo-blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">william</div><div class="author-info__description"> 青山不改，绿水长流</div></div><div class="card-info-data site-data is-center"><a href="/my-hexo-blog/archives/"><div class="headline">文章</div><div class="length-num">17</div></a><a href="/my-hexo-blog/tags/"><div class="headline">标签</div><div class="length-num">13</div></a><a href="/my-hexo-blog/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/yokeney"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/yokeney" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="/my-hexo-blog/965660471@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">加油</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#react%E5%8E%9F%E7%90%86"><span class="toc-number">1.</span> <span class="toc-text">react原理</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%9F%A5%E8%AF%86%E7%82%B9"><span class="toc-number">2.</span> <span class="toc-text">知识点</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%99%9A%E6%8B%9Fdom"><span class="toc-number">2.1.</span> <span class="toc-text">虚拟dom</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98%EF%BC%9Areact-virtual-dom%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F%E8%AF%B4%E4%B8%80%E4%B8%8Bdiff%E7%AE%97%E6%B3%95%EF%BC%9F"><span class="toc-number">2.2.</span> <span class="toc-text">常见问题：react virtual dom是什么？说一下diff算法？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BC%A0%E7%BB%9Fdom%E6%B8%B2%E6%9F%93%E6%B5%81%E7%A8%8B"><span class="toc-number">2.3.</span> <span class="toc-text">传统dom渲染流程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JSX"><span class="toc-number">2.4.</span> <span class="toc-text">JSX</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E4%BB%80%E4%B9%88%E6%98%AFJSX"><span class="toc-number">2.4.1.</span> <span class="toc-text">1. 什么是JSX</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E4%B8%BA%E4%BB%80%E4%B9%88%E9%9C%80%E8%A6%81JSX"><span class="toc-number">2.4.2.</span> <span class="toc-text">2. 为什么需要JSX</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-React-16%E5%8E%9F%E7%90%86"><span class="toc-number">2.4.3.</span> <span class="toc-text">3. React 16原理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-React-17%E5%8E%9F%E7%90%86"><span class="toc-number">2.4.4.</span> <span class="toc-text">4. React 17原理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-%E4%B8%8Evue%E7%9A%84%E5%BC%82%E5%90%8C%EF%BC%9A"><span class="toc-number">2.4.5.</span> <span class="toc-text">5. 与vue的异同：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A0%B8%E5%BF%83%E7%9A%84api"><span class="toc-number">2.4.6.</span> <span class="toc-text">核心的api</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ReactDOM"><span class="toc-number">2.4.7.</span> <span class="toc-text">ReactDOM</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8A%82%E7%82%B9%E7%B1%BB%E5%9E%8B"><span class="toc-number">2.4.8.</span> <span class="toc-text">节点类型</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B3%A8%E6%84%8F%E8%8A%82%E7%82%B9%E7%B1%BB%E5%9E%8B%EF%BC%9A"><span class="toc-number">2.4.8.1.</span> <span class="toc-text">注意节点类型：</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E7%BB%84%E4%BB%B6"><span class="toc-number">2.5.</span> <span class="toc-text">类组件</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%B1%BB%E7%BB%84%E4%BB%B6%E6%BA%90%E7%A0%81"><span class="toc-number">2.5.1.</span> <span class="toc-text">类组件源码</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ReactDOM-render"><span class="toc-number">2.5.2.</span> <span class="toc-text">ReactDOM.render</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Component"><span class="toc-number">2.6.</span> <span class="toc-text">Component</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#reconciliation%E5%8D%8F%E8%B0%83"><span class="toc-number">2.7.</span> <span class="toc-text">reconciliation协调</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AE%BE%E8%AE%A1%E5%8A%A8%E5%8A%9B"><span class="toc-number">2.7.1.</span> <span class="toc-text">设计动力</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#diff-%E7%AD%96%E7%95%A5"><span class="toc-number">2.7.2.</span> <span class="toc-text">diff 策略</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#diff%E8%BF%87%E7%A8%8B"><span class="toc-number">2.8.</span> <span class="toc-text">diff过程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#fiber"><span class="toc-number">2.9.</span> <span class="toc-text">fiber</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88%E9%9C%80%E8%A6%81fiber"><span class="toc-number">2.9.1.</span> <span class="toc-text">为什么需要fiber</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E4%BB%BB%E5%8A%A1%E5%88%86%E8%A7%A3%E7%9A%84%E6%84%8F%E4%B9%89"><span class="toc-number">2.9.2.</span> <span class="toc-text">2. 任务分解的意义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AFfiber"><span class="toc-number">2.9.3.</span> <span class="toc-text">什么是fiber</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#callback"><span class="toc-number">2.9.3.1.</span> <span class="toc-text">callback</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#options-%E5%8F%AF%E9%80%89"><span class="toc-number">2.9.3.2.</span> <span class="toc-text">options 可选</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#createFiber-js"><span class="toc-number">2.10.</span> <span class="toc-text">createFiber.js</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#react-dom-js"><span class="toc-number">2.10.1.</span> <span class="toc-text">react-dom.js</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ReactFiberWorkLoop-js"><span class="toc-number">2.10.2.</span> <span class="toc-text">ReactFiberWorkLoop.js</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#ReactFiberReconciler-js"><span class="toc-number">2.11.</span> <span class="toc-text">ReactFiberReconciler.js</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#utils-js"><span class="toc-number">2.11.1.</span> <span class="toc-text">utils.js</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/my-hexo-blog/2023/05/26/VUE3%E9%9B%86%E6%88%90%E4%BC%81%E4%B8%9A%E7%BA%A7%E5%90%8E%E5%8F%B0%E9%80%9A%E7%94%A8%E8%A7%84%E8%8C%83%E6%A8%A1%E7%89%88/" title="VUE3集成企业级后台通用规范模版"><img src="/my-hexo-blog/img/1.png" onerror="this.onerror=null;this.src='/my-hexo-blog/img/404.jpg'" alt="VUE3集成企业级后台通用规范模版"/></a><div class="content"><a class="title" href="/my-hexo-blog/2023/05/26/VUE3%E9%9B%86%E6%88%90%E4%BC%81%E4%B8%9A%E7%BA%A7%E5%90%8E%E5%8F%B0%E9%80%9A%E7%94%A8%E8%A7%84%E8%8C%83%E6%A8%A1%E7%89%88/" title="VUE3集成企业级后台通用规范模版">VUE3集成企业级后台通用规范模版</a><time datetime="2023-05-26T13:17:48.000Z" title="发表于 2023-05-26 21:17:48">2023-05-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-hexo-blog/2023/04/24/%E6%B2%A1%E7%9C%8B%E8%BF%99%E7%AF%87%E6%96%87%E7%AB%A0%E4%B9%8B%E5%89%8D%E5%8E%9F%E6%9D%A5%E6%88%91%E5%AF%B9react18%E4%B8%80%E7%9F%A5%E5%8D%8A%E8%A7%A3/" title="没看这篇文章之前原来我对react18一知半解"><img src="/my-hexo-blog/img/react.png" onerror="this.onerror=null;this.src='/my-hexo-blog/img/404.jpg'" alt="没看这篇文章之前原来我对react18一知半解"/></a><div class="content"><a class="title" href="/my-hexo-blog/2023/04/24/%E6%B2%A1%E7%9C%8B%E8%BF%99%E7%AF%87%E6%96%87%E7%AB%A0%E4%B9%8B%E5%89%8D%E5%8E%9F%E6%9D%A5%E6%88%91%E5%AF%B9react18%E4%B8%80%E7%9F%A5%E5%8D%8A%E8%A7%A3/" title="没看这篇文章之前原来我对react18一知半解">没看这篇文章之前原来我对react18一知半解</a><time datetime="2023-04-24T09:11:02.000Z" title="发表于 2023-04-24 17:11:02">2023-04-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-hexo-blog/2023/04/24/%E5%8F%B2%E4%B8%8A%E6%9C%80%E5%85%A8Vue3%E6%80%BB%E7%BB%93/" title="史上最全Vue3总结"><img src="/my-hexo-blog/img/1.png" onerror="this.onerror=null;this.src='/my-hexo-blog/img/404.jpg'" alt="史上最全Vue3总结"/></a><div class="content"><a class="title" href="/my-hexo-blog/2023/04/24/%E5%8F%B2%E4%B8%8A%E6%9C%80%E5%85%A8Vue3%E6%80%BB%E7%BB%93/" title="史上最全Vue3总结">史上最全Vue3总结</a><time datetime="2023-04-24T09:01:19.000Z" title="发表于 2023-04-24 17:01:19">2023-04-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-hexo-blog/2023/04/24/%E4%B8%8EReact18%E7%9A%84%E5%8C%BA%E5%88%AB%E6%B7%B1%E5%BA%A6%E8%A7%A3%E6%9E%90/" title="Vue与React18的区别深度解析"><img src="/my-hexo-blog/img/vue_vs-react.png" onerror="this.onerror=null;this.src='/my-hexo-blog/img/404.jpg'" alt="Vue与React18的区别深度解析"/></a><div class="content"><a class="title" href="/my-hexo-blog/2023/04/24/%E4%B8%8EReact18%E7%9A%84%E5%8C%BA%E5%88%AB%E6%B7%B1%E5%BA%A6%E8%A7%A3%E6%9E%90/" title="Vue与React18的区别深度解析">Vue与React18的区别深度解析</a><time datetime="2023-04-24T08:39:32.000Z" title="发表于 2023-04-24 16:39:32">2023-04-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-hexo-blog/2022/10/25/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E5%86%99%E4%B8%80%E4%B8%AAbabel/" title="手把手教你写babel插件"><img src="/my-hexo-blog/img/babel.webp" onerror="this.onerror=null;this.src='/my-hexo-blog/img/404.jpg'" alt="手把手教你写babel插件"/></a><div class="content"><a class="title" href="/my-hexo-blog/2022/10/25/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E5%86%99%E4%B8%80%E4%B8%AAbabel/" title="手把手教你写babel插件">手把手教你写babel插件</a><time datetime="2022-10-25T01:10:14.000Z" title="发表于 2022-10-25 09:10:14">2022-10-25</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/my-hexo-blog/img/1.png')"><div id="footer-wrap"><div class="copyright">&copy;2018 - 2023 By william</div><div class="footer_custom_text">关山难越，谁悲失路之人</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/my-hexo-blog/js/utils.js"></script><script src="/my-hexo-blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="/my-hexo-blog/js/search/local-search.js"></script><div class="js-pjax"></div><div class="aplayer no-destroy" data-id="22712173" data-server="netease" data-mini="true" data-type="song" data-autoplay="false" data-lrcType="-1"> </div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/metingjs/dist/Meting.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>